package zookeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.PropertyValues;

public class ZookeeperClient2 implements Watcher {

    private static final String host = "127.0.0.1:2181";

    private static CountDownLatch countDownLatch = new CountDownLatch(10000);

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zooKeeper = new ZooKeeper(host, 500000, new ZookeeperClient2());
        PropertyValues f;
        zooKeeper.getData("/fff", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("///////");
            }
        }, null);

        zooKeeper.getChildren("/fff", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("///getChildren");
            }
        }, null);

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println("receive watched event: " + watchedEvent);
        System.out.println("watchedEvent Type : " + watchedEvent.getType());
        if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
            System.out.println("连接到zookeeper");
        }
        countDownLatch.countDown();
    }
}
